package com.chen.ip;

import java.net.InetAddress;
import java.net.UnknownHostException;

// IP相关的Java类InetAddress
public class IPTest {
    public static void main(String[] args) {

        try {
            // 查看本机地址
            InetAddress address = InetAddress.getByName("127.0.0.1");
            InetAddress address2 = InetAddress.getByName("localhost");
            InetAddress localHost = InetAddress.getLocalHost();
            System.out.println(address);
            System.out.println(address2);
            // 主机名/IP地址
            System.out.println(localHost);

            // 查询网址的IP地址
            InetAddress baidu = InetAddress.getByName("www.baidu.com");
            System.out.println(baidu);

            // 常用的方法？
            System.out.println(baidu.getHostName());// 主机名
            System.out.println(baidu.getHostAddress());// 主机IP地址

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }

    }
}
